﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WaterRevenueMS.WebUI.Common;
using System.ComponentModel.Composition;
using WaterRevenueMS.Core.Service.BasicSet.Impl;
using WaterRevenueMS.WebUI.Extension.Filters;
using WaterRevenueMS.Domain.Common.Models;
using WaterRevenueMS.Core.Service.BasicSet;
using WaterRevenueMS.Domain.ViewModels.BasicSet.KindOfWaterUse;
using WaterRevenueMS.Domain.ViewModels.BasicSet.AdditionItem;
using WaterRevenueMS.Componnent.Entities.BasicSet;
using System.Linq.Expressions;
using WaterRevenueMS.WebUI.Extension.MvcHelper;
using WaterRevenueMS.Componnent.Common;

namespace WaterRevenueMS.WebUI.Controllers
{
    [Export]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class AdditionItemController : AdminController
    {
        #region 属性
        [Import]
        public IKindOfWaterUseService KindOfWaterUseService { get; set; }

        [Import]
        public IAdditionItemService AdditionItemService { get; set; }
        #endregion

        [AdminLayoutAttribute]
        public ActionResult Index()
        {
            var model = new AdditionItemModel();
            #region 用水性质
            var parentModuleData = KindOfWaterUseService.KindOfWaterUses.Where(t =>  t.IsDeleted == false)
            .Select(t => new AdditionItemModel
            {
                Id = t.Id,
                Yong_SLX_Name = t.Yong_SXZ
            });
            foreach (var item in parentModuleData)
            {
                model.Search.Yong_SLX_NoItems.Add(new SelectListItem { Text = item.Yong_SLX_Name, Value = item.Id.ToString() });
            }
            #endregion

         
            return View(model);
        }
        [AdminPermission(PermissionCustomMode.Ignore)]
        public ActionResult List(DataTableParameter param)
        {
            int total = AdditionItemService.AdditionItems.Count(t => t.IsDeleted == false);

            var expr = BuildSearchCriteria();

            var filterResult = AdditionItemService.AdditionItems.Where(expr).Select(t => new AdditionItemModel
            {
                Id = t.Id,
                Yong_SLX_Name = t.KindOfWaterUse != null ? t.KindOfWaterUse.Yong_SXZ : "",
                Fu_JXM = t.Fu_JXM,
                Fu_JFDJ = t.Fu_JFDJ,
                OrderNo = t.OrderNo

            }).OrderBy(t => t.Id).Skip(param.iDisplayStart).Take(param.iDisplayLength).ToList();

            int sortId = param.iDisplayStart + 1;

            var result = from c in filterResult
                         select new[]
                        {
                            sortId++.ToString(), 
                            c.Fu_JXM,
                            c.Fu_JFDJ.ToString(),
                            c.Yong_SLX_Name,
                            c.OrderNo.ToString(),
                            c.Id.ToString()
                        };

            return Json(new
            {
                sEcho = param.sEcho,
                iDisplayStart = param.iDisplayStart,
                iTotalRecords = total,
                iTotalDisplayRecords = total,
                aaData = result
            }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Create()
        {
            var model = new AdditionItemModel();
            InitAdditionItemModel(model);
            return PartialView(model);
        }

        [HttpPost]
        [AdminOperateLog]
        public ActionResult Create(AdditionItemModel model)
        {
            InitAdditionItemModel(model);
            if (ModelState.IsValid)
            {
                OperationResult result = AdditionItemService.Insert(model);
                if (result.ResultType == OperationResultType.Success)
                {
                    return Json(result);
                }
                else
                {
                    return PartialView(model);
                }
            }
            else
            {
                return PartialView(model);
            }
        }

        public ActionResult Edit(int Id)
        {
            var model = new AdditionItemModel();
            InitAdditionItemModel(model);
            var entity = AdditionItemService.AdditionItems.FirstOrDefault(t => t.Id == Id);
            if (null != entity)
            {
                model = new AdditionItemModel
                {
                    Id = entity.Id,
                    Yong_SLX_No = entity.Yong_SLX_No,
                    Fu_JXM =entity.Fu_JXM,
                    Fu_JFDJ = entity.Fu_JFDJ,
                    OrderNo = entity.OrderNo

                };
            }
            return PartialView(model);
        }

        [HttpPost]
        public ActionResult Edit(AdditionItemModel model)
        {
            InitAdditionItemModel(model);
            if (ModelState.IsValid)
            {
                
                OperationResult result = AdditionItemService.Update(model);
                if (result.ResultType == OperationResultType.Success)
                {
                    return Json(result);
                }
                else
                {
                    return PartialView(model);
                }

            }
            else
            {
                return PartialView(model);
            }
        }


        public ActionResult Delete(int Id)
        {
            OperationResult result = AdditionItemService.Delete(Id);
            return Json(result);
        }


        private Expression<Func<AdditionItem, Boolean>> BuildSearchCriteria()
        {
            DynamicLambda<AdditionItem> bulider = new DynamicLambda<AdditionItem>();
            Expression<Func<AdditionItem, Boolean>> expr = null;
            if (!string.IsNullOrEmpty(Request["Fu_JXM"]))
            {
                var data = Request["Fu_JXM"].Trim();
                Expression<Func<AdditionItem, Boolean>> tmp = t => t.Fu_JXM.Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }
            if (!string.IsNullOrEmpty(Request["Fu_JFDJ"]) && Request["Fu_JFDJ"] != "0")
            {
                var data = Request["Fu_JFDJ"].Trim();
                Expression<Func<AdditionItem, Boolean>> tmp = t => t.Fu_JFDJ.ToString().Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }
            if (!string.IsNullOrEmpty(Request["Yong_SLX_No"]) && Request["Yong_SLX_No"] != "0")
            {
                var data = Convert.ToInt32(Request["Yong_SLX_No"]);
                Expression<Func<AdditionItem, Boolean>> tmp = t => (t.Id == data) || (t.Yong_SLX_No == data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }

            Expression<Func<AdditionItem, Boolean>> tmpSolid = t => t.IsDeleted == false;
            expr = bulider.BuildQueryAnd(expr, tmpSolid);

            return expr;
        }
        public  void InitAdditionItemModel(AdditionItemModel model)
        {
   
            var parentModuleData = KindOfWaterUseService.KindOfWaterUses.Where(t =>  t.IsDeleted == false)
            .Select(t => new AdditionItemModel
            {
                Id = t.Id,
                Yong_SLX_Name = t.Yong_SXZ
            });
            foreach (var item in parentModuleData)
            {
                model.Yong_SLX_NoItems.Add(new SelectListItem { Text = item.Yong_SLX_Name, Value = item.Id.ToString() });
            }
    }

    }
}
